﻿using System.Collections.Generic;
using UnityEngine;

namespace UFrame
{
    /// <summary>
    /// 高级音效配置
    /// </summary>
    public static class AudioAdvanced
    {
        /// <summary>
        /// 如果有 排斥音效在播放，就不播放此音效
        /// </summary>
        public static bool IsNoMuteAudio(AudioConfig audioConfig,List<AudioData> mAudioDataList)
        {
            if (string.IsNullOrEmpty(audioConfig.mutexAudioNoPlay))
            {
                return false;
            }

            var fileNameAry = audioConfig.mutexAudioNoPlay.Split(':');
            foreach (var item in mAudioDataList)
            {
                string a = item.AS.clip.name;

                foreach (var fileName in fileNameAry)
                {
                    if (a == fileName)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        ///  排斥音效在播放，都停止掉
        /// </summary>
        public static bool IsMuteAudio(AudioConfig audioConfig,List<AudioData> mAudioDataList)
        {
            if (string.IsNullOrEmpty(audioConfig.mutexAudioPlay))
            {
                return false;
            }

            var fileNameAry = audioConfig.mutexAudioPlay.Split(':');

            foreach (var item in mAudioDataList)
            {
                string a = item.AS.clip.name;

                foreach (var fileName in fileNameAry)
                {
                    if (a == fileName)
                    {
                        item.AS.Stop();
                        break;
                    }
                }
            }

            return true;
        }

        /// <summary>
        /// 根据时间忽略音效播放
        /// </summary>
        public static bool IsIgnoreAudio(AudioConfig audioConfig,Dictionary<string, float> ignoreTime)
        {
            
            if (audioConfig.ignoreTime > 0)
            {
                var n = audioConfig.fileName;
                if (!ignoreTime.ContainsKey(n))
                {
                    ignoreTime[n] = Time.time;
                    return false;              //可以播放
                }

                float ign = ignoreTime[n];
                if (Time.time - ign > audioConfig.ignoreTime)
                {
                    ignoreTime[n] = Time.time;
                    return false;              //可以播放
                }

                return true;                   
            }

            return false;
        }

    }
}